package com.itlong.cloud.controller.operate;


import com.itlong.cloud.POJO.DTO.operate.OperateSaveSipClusterDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateSipClusterPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateSipClusterVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.operate.IOperateSipClusterServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营—运营管理模块—SIP集群管理。
 * <desc/>
 *
 * @createDate 2017/10/19.
 */
@RestController
@RequestMapping("/marketer/sipCluster")
public class OperateSipManageController  extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateSipManageController.class);

    //SIP集群管理
    @Autowired
    IOperateSipClusterServiceClient iOperateSipClusterServiceClient;

    /**
     * <desc>
     *      添加SIP服务器信息。
     * <desc/>
     *
     * @param saveDTO SIP数据传输对象
     * @return
     * @author shengen.T
     * @createDate 2017/9/30
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.SAVE_SIPSERVICE_INFO_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "/saveSipInfo" , method = RequestMethod.POST)
    public Object saveSipInfo(OperateSaveSipClusterDTO saveDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            saveDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateSaveSipClusterDTO.class,request);
            if(null == saveDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断SIP服务器的名字
            if(StringUtils.isBlank(saveDTO.getSipName())){
                return new MessageVO(OperateErrorCodeEnum.SIP_NAME_EMPTY_ERR.getErrorCode());
            }
            //判断服务器地址
            if(StringUtils.isBlank(saveDTO.getSipAddress())){
                return new MessageVO(OperateErrorCodeEnum.SIP_ADDRESS_EMPTY_ERR.getErrorCode());
            }
            //判断端口
            if(null == saveDTO.getSipPort()){
                return new MessageVO(OperateErrorCodeEnum.SIP_PORT_EMPTY_ERR.getErrorCode());
            }
            //判断国家ID
            if(StringUtils.isBlank(saveDTO.getCountryId())){
                return new MessageVO(OperateErrorCodeEnum.COUNTRY_ID_EMPTY_ERR.getErrorCode());
            }
            //判断区域
            if(StringUtils.isBlank(saveDTO.getAreaId())){
                return new MessageVO(OperateErrorCodeEnum.SIP_DOMAIN_EMPTY_ERR.getErrorCode());
            }
            //判断穿透服务器地址
            if(StringUtils.isBlank(saveDTO.getTurnServer())){
                return new MessageVO(OperateErrorCodeEnum.SIP_TURNSERVICE_EMPTY_ERR.getErrorCode());
            }
            iOperateSipClusterServiceClient.saveSipInfo(saveDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_SIP集群save】添加SIP服务器信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      修改SIP服务器信息。
     * <desc/>
     *
     * @param saveDTO SIP数据传输对象
     * @return
     * @author shengen.T
     * @createDate 2017/9/30
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_SIPSERVICE_INFO_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "/updateSipInfo" , method = RequestMethod.POST)
    public Object updateSipInfo(OperateSaveSipClusterDTO saveDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            saveDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateSaveSipClusterDTO.class,request);
            if(null == saveDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断SIP服务器ID
            if(StringUtils.isBlank(saveDTO.getSipId())){
                return new MessageVO(OperateErrorCodeEnum.SIP_ID_EMPTY_ERR.getErrorCode());
            }
            //判断SIP服务器的名字
            if(StringUtils.isBlank(saveDTO.getSipName())){
                return new MessageVO(OperateErrorCodeEnum.SIP_NAME_EMPTY_ERR.getErrorCode());
            }
            //判断服务器地址
            if(StringUtils.isBlank(saveDTO.getSipAddress())){
                return new MessageVO(OperateErrorCodeEnum.SIP_ADDRESS_EMPTY_ERR.getErrorCode());
            }
            //判断端口
            if(null == saveDTO.getSipPort()){
                return new MessageVO(OperateErrorCodeEnum.SIP_PORT_EMPTY_ERR.getErrorCode());
            }
            //判断国家ID
            if(StringUtils.isBlank(saveDTO.getCountryId())){
                return new MessageVO(OperateErrorCodeEnum.COUNTRY_ID_EMPTY_ERR.getErrorCode());
            }
            //判断区域
            if(StringUtils.isBlank(saveDTO.getAreaId())){
                return new MessageVO(OperateErrorCodeEnum.SIP_DOMAIN_EMPTY_ERR.getErrorCode());
            }
            //判断穿透服务器地址
            if(StringUtils.isBlank(saveDTO.getTurnServer())){
                return new MessageVO(OperateErrorCodeEnum.SIP_TURNSERVICE_EMPTY_ERR.getErrorCode());
            }
            iOperateSipClusterServiceClient.updateSipInfo(saveDTO);
            messageVO = new MessageVO(saveDTO.getSipId(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_SIP集群update】修改SIP服务器信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      设置SIP服务器使用状态。
     * <desc/>
     *
     * @param  sipId SIP服务器ID
     * @param  status  状态：启用/禁用
     * @return
     * @author shengen.T
     * @createDate 2017/9/30
     */
    /*  启用禁用暂时关闭
    @LoggerInfo(operateDesc = LogDescConstant.SET_SIPSERVICE_STATUS_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "/setSipStatus" , method = RequestMethod.POST)
    public Object setSipStatus(String sipId,Integer status) {
        MessageVO messageVO;
        try {
            if(StringUtils.isBlank(sipId)){
                return new MessageVO(OperateErrorCodeEnum.SIP_ID_EMPTY_ERR.getErrorCode());
            }
            //判断状态
            if((null == status) || (status != DataStatusEnum.NORMAL.getType() &&
                    status != DataStatusEnum.BAN.getType())){
                return new MessageVO(OperateErrorCodeEnum.SIP_STATUS_ANVAID_ERR.getErrorCode());
            }
            iOperateSipClusterServiceClient.setSipStatus(sipId,status);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_SIP集群setStatus】修改SIP服务器使用状态启用/禁用失败",e);
        }
        return messageVO;
    }
     */

    /**
     * <desc>
     *      根据SIP服务器ID获取单个SIP服务器信息。
     * <desc/>
     *
     * @param  sipId  SIP服务器ID
     * @return
     * @author shengen.T
     * @createDate 2017/9/30
     */
    @AuthAccessRequired
    @ResponseBody
    @RequestMapping(path = "/getSipInfo" , method = RequestMethod.POST)
    public Object getSipInfo(String sipId){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            sipId=request.getAttribute("sipId")==null?null:request.getAttribute("sipId").toString();
            if(StringUtils.isBlank(sipId)){
                return new MessageVO(OperateErrorCodeEnum.SIP_ID_EMPTY_ERR.getErrorCode());
            }
            OperateSipClusterVO sipInfo = iOperateSipClusterServiceClient.getSipInfo(sipId);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),sipInfo);
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_SIP集群getOne】获取单个SIP服务器信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取SIP服务器分页信息。
     * <desc/>
     *
     * @param pageDTO 请求参数
     * @return
     * @author shengen.T
     * @createDate 2017/9/30
     */
    @AuthAccessRequired
    @RequestMapping(path = "/getSipInfoPage" , method = RequestMethod.POST)
    @ResponseBody
    public  Object getSipInfoPage(OperateSipClusterPageDTO pageDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO=HttpProtocolUtil.parseRequestParamToDTO(OperateSipClusterPageDTO.class,request);
            if(null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateSipClusterVO> sipInfoPage = iOperateSipClusterServiceClient.getSipInfoPage(pageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),sipInfoPage);
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_SIP集群getPage】获取SIP服务器分页信息失败",e);
        }
        return messageVO;
    }



}
